<?php

class Default_IndexController extends Zend_Controller_Action
{

    public function init()
    {
        $option = array(
        		'layout'=>'layout',
        		'layoutPath'=>APPLICATION_PATH .'/layouts/scripts/default/',
        );
        Zend_Layout::startMvc($option);

        $this->_model_manu = new Application_Model_Manufacture();
        $this->_model_product = new Application_Model_Product();
    }

    //show all products to website
    public function indexAction()
    {
        $select = $this->_model_product->Get_All_Product();
        $this->view->headTitle("Trang chủ");
        $this->view->products = $this->Pag($select);
    }

    //show all manufacture in database to website
    public function manufactureAction()
    {
        
        $manu = $this->_model_manu->Get_All_Manufacture();
        $this->view->manu = $manu;
    }

    public function newproductAction()
    {
        $new_product = $this->_model_product->Get_3_Product_Newest();
        $this->view->new_product = $new_product;
    }

    public function searchAction()
    {
        $form = new Default_Form_Search();
        foreach ($this->_model_manu->Get_All_Manufacture() as $item_manu){
        	$array[$item_manu->manufacture_id]=$item_manu->manufacture_name;
        }
        $form->searchmanu->addMultiOptions($array);
        $this->view->form_search = $form;
    }

    //show header of webpage
    public function headerAction()
    {
        $manu = $this->_model_manu->Get_All_Manufacture();
        $this->view->manu = $manu;
        $model_banner = new Application_Model_Banner(); 
        $this->view->list_banner = $model_banner->Get_All_Banner();
        $lang = $this->_request->getParam('lang');
        if($lang){
            if($lang == 'vi')
                $show_lang = 'English';
            else 
                $show_lang = 'Viet Nam';
            $this->view->lang = $show_lang;
            echo $lang;
        }
            
    }

    //show products with manufacture_id or search product
    public function productAction()
    {
        $manufacture_id = $this->_request->getParam('id');
        $keyword = $this->_request->getParam('keyword');
        $manufacture = $this->_request->getParam('searchmanu');
        $pricefrom = $this->_request->getParam('pricefrom');
        $priceto = $this->_request->getParam('priceto');
        
        if(!empty($manufacture_id)){
        	$select = $this->_model_product->Get_Product_by_ManufactureID($manufacture_id);
        	$this->view->headTitle($this->_model_manu->Get_Manufacture_by_ID($manufacture_id)->manufacture_name);
        	$this->view->products = $this->Pag($select);
        	$this->view->id = $manufacture_id;
        }
        else if(!empty($keyword)
            || !empty($searchmanu)
            || !empty($pricefrom)
            || !empty($priceto))
        {
            $select = $this->_model_product->Search_Product($keyword, $manufacture, $pricefrom, $priceto);
            $this->view->headTitle("Tìm kiếm sản phẩm"); 
            $this->view->products = $this->Pag($select);
        }
        
    }

    //paginator
    private function Pag($select)
    {
        $adapter = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($adapter);
        $paginator->setItemCountPerPage(9);
        $pageNumber = $this->_request->getParam('page', 1);
        $paginator->setCurrentPageNumber($pageNumber);
        return $paginator;
    }

    


}













